package org.gdstash.ui.select;

import org.gdstash.db.SelectionCriteria;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;

public class SlotSelectionPane extends AdjustablePanel {
    private WeaponTypePane pnlWeaponTypes;
    private ArmorTypePane pnlArmorTypes;

    public SlotSelectionPane() {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlWeaponTypes)).addGroup(layout.createParallelGroup().addComponent(this.pnlArmorTypes));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlWeaponTypes).addComponent(this.pnlArmorTypes));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlWeaponTypes, this.pnlArmorTypes);
        layout.linkSize(1, this.pnlWeaponTypes, this.pnlArmorTypes);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SLOT_INFO"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.pnlWeaponTypes == null) {
            this.pnlWeaponTypes = new WeaponTypePane();
        } else {
            this.pnlWeaponTypes.adjustUI();
        }
        if (this.pnlArmorTypes == null) {
            this.pnlArmorTypes = new ArmorTypePane();
        } else {
            this.pnlArmorTypes.adjustUI();
        }
    }

    public void clear() {
        this.pnlWeaponTypes.clear();
        this.pnlArmorTypes.clear();
    }

    public void addCriteria(final SelectionCriteria criteria) {
        criteria.selMode = SelectionCriteria.SelectionMode.COMPONENT;
        this.pnlWeaponTypes.addComponentCriteria(criteria);
        this.pnlArmorTypes.addComponentCriteria(criteria);
    }
}
